home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1056 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  82 lines

  1. Path: magnus.acs.ohio-state.edu!csn!carbon!ouray!exli
  2. From: exli@ouray.cudenver.edu (ELLIE XIAO-YU LI)
  3. Newsgroups: comp.lang.c
  4. Subject: HELP NEEDED: What the hell is wrong with my program?
  5. Date: 11 Jan 1996 01:55:37 GMT
  6. Organization: University of Colorado at Denver
  7. Message-ID: <4d1qmp$h43@carbon.cudenver.edu>
  8. NNTP-Posting-Host: ouray.cudenver.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11.  
  12. first of all, i have to thank all of you who have answered me through email
  13. or have followed up my post.  and here is more details about my problem.
  14.  
  15. the system i am using is a dec osf/1.  the code is something like:
  16.  
  17. #include <string.h>
  18.  
  19. typedef struct {
  20.     .
  21.     .
  22.     .
  23.     char *name;
  24.     .
  25.     .
  26.     .
  27. } aStruct;
  28.  
  29. aStruct *ptr;
  30. char *str1="this is a test";
  31. char *str2="this is another test";
  32.  
  33. ptr=(aStruct *)malloc(sizeof(aStruct));
  34. ptr->name=(char *)malloc(strlen(str1));
  35. strcpy(ptr->name, str1);            /*no problem here*/
  36. ...
  37. /*then later i want  ptr->name to point to another string*/
  38. free(ptr->name);
  39. ptr->name=(char *)malloc(strlen(str2));
  40. strcpy(ptr->name, str2);            /*problem arised here*/
  41.  
  42.  
  43. i have a struct which one of its fields is a char *.  i want to allocate
  44. memory for it dynamically becuase i don't know the size of the string it
  45. will point to.  so i cannot use an array of char.  the first time i
  46. allocateed memory for it and copied the string into the newly allocated memory
  47. was perfectly ok.  but later in the program i wanted to change the string it
  48. points to.  so i freed up the memory and reallocated another memory block
  49. and copied the new string into it.  then i got thousands of  "unalign access"
  50. messages runing on my screen and the program hangs.  i don't know what i did
  51. wrong here.  i tried using the -Wcast-align option (i am using gcc, btw) to
  52. compile my program but the compiler didn't give me any warning about that.
  53.  
  54. many of you who replied to me saud that it may be some kind of memory
  55. address problem as some of the machines require certain data types be stored
  56. in an even address.  so i think it's the system's problem and not my
  57. program's cause i used the malloc function to allocate memory for my
  58. pointers.  if it's not allowed to put the data in an odd address, then why
  59. the malloc function would do that?  it should be properly aligned by the
  60. system before this function returns a pointer to a variable.  so i still can't
  61. figure out what the problem is.
  62.  
  63. so what i did is freed up the entire struct and do the assignments again and
  64. it finally runs smoothly.
  65.  
  66. free(ptr);
  67. ptr=(aStruct *)malloc(sizeof(aStruct));
  68. ...                    /*redo all the assigments here*/
  69. ptr->name=(char *)malloc(strlen(str2));
  70. strcpy(ptr->name, str2);        /*finally get it to change to a new value*/
  71.  
  72. i don't know if this is the right solution for it.  any more insight about
  73. this?  i tried to free up ptr->name and allocate memory for it again after
  74. it pointed to str2 as a test, and it failed as the first time.  i am
  75. thinking about making the ptr->name NULL after the free function call but i
  76. haven't tried it yet.
  77.  
  78.  
  79.  
  80. Mitch
  81. exli@ouray.cudenver.edu
  82.